<template>
    <div class="layout-padding">
        <el-form :inline="true" ref="queryRef" :model="queryParams" label-width="120px">
            <el-form-item label="区段：">
                <el-tree-select v-model="queryParams.section" :data="dataSource" clearable 
                    :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                    :render-after-expand="false" check-strictly @change="treeChange">
                </el-tree-select>
            </el-form-item>
            <el-form-item label="工区：">
                <el-tree-select v-model="queryParams.workArea" :data="dataSourceArea" clearable
                    :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                    :render-after-expand="false" check-strictly>
                </el-tree-select>
            </el-form-item>
            <el-form-item label="上道地点：">
                <el-input placeholder="请输入内容" v-model="queryParams.upperLocaltion">
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
                <el-button type="primary" @click="handleAdd">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="list">
            <el-table-column type="index" width="50" label="序号"></el-table-column>
            <el-table-column label="工区" align="center" prop="workAreaName" />
            <el-table-column label="上道地点" align="center" prop="upperLocaltion" />
            <el-table-column label="线别" align="center" prop="lineType" />
            <el-table-column label="公里标" align="center" prop="kilometer" />
            <el-table-column label="路径规划" width="180" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleView(scope.row)">查看</el-button>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 详情对话框 -->
        <el-dialog :title="title" v-model="open" width="780px" append-to-body>
            <el-form ref="configRef" :model="form" :rules="rules" label-width="90px" :disabled="viewFlag">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="区段：" prop="section">
                            <el-tree-select v-model="form.section" :data="dataSource"
                                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                :render-after-expand="false" check-strictly @change="treeChange2">
                            </el-tree-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工区：" prop="workArea">
                            <el-tree-select v-model="form.workArea" :data="dataSourceArea2"
                                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                :render-after-expand="false" check-strictly>
                            </el-tree-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="上道地点：" prop="upperLocaltion">
                            <el-input v-model="form.upperLocaltion" placeholder="请输入参数" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="线别：" prop="lineType">
                            <el-select clearable v-model="form.lineType" placeholder="请选择">
                                <el-option v-for="item in lineTypes" :key="item" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="公里标：" prop="kilometer">
                            <el-input v-model="form.kilometer" placeholder="请输入参数" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                    <!-- 地图容器 -->
                <div class="mapBox" v-for="(item, index) in form.paths" :key="index">
                    <span v-if="form.paths.length > 0">{{ "路径" + Number(index + 1) }}</span>
                    <el-button type="primary" class="mapDel" @click="delMapPath(item)" v-if="!viewFlag">删除</el-button>
                    <Map ref="mapRef" :mapId="'lineMap' + index" :lineData="item.locationMap"
                        style="height: 100%;width: 100%; "></Map>
                </div>
                <!-- <div style="display: flex;justify-content: center;">
                    <el-icon style="font-size: 40px" @click="addMapPath">
                        <Plus />
                    </el-icon>
                </div> -->
            </el-form>
            <template #footer v-if="!viewFlag">
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">保存</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listUpperLocation, getUpperLocation, addUpperLocation, updateUpperLocation, delUpperLocation } from "@/api/system/EmergencyPath.js";
import { getTreeList, getDeptBy } from "@/api/work";
import Map from "@/components/Map"
const mapNum = ref([])
const dataArr = ref([])
// import lineData2 from '@/utils/data.js' // 模拟数据
import GPS from '@/utils/change.js' // 解决坐标偏移
// lineData2.lineData.forEach((i) => {
//     let item = i[0]
//     i[0] = i[1]
//     i[1] = item
//     i = GPS.gcj_encrypt(
//         Number(i[0]),
//         Number(i[1])
//     )
//     dataArr.value.push(i)
// })

console.log("dataArr", dataArr.value)

const { proxy } = getCurrentInstance();
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        submited: true,
        workAreaName: undefined,
        upperLocaltion: undefined
    },
    rules: {
        // section: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
    },
    tempForm: {}
});
const { queryParams, form, rules, tempForm } = toRefs(data);


const lineTypes = [
    {
        value: '上行',
        label: '上行',
    },
    {
        value: '下行',
        label: '下行',
    }
]

const loading = ref(true);
const viewFlag = ref(false);
const list = ref([]);
const total = ref(0);
const open = ref(false);
const title = ref("应急路径");

const lineData = ref([]);


//区段数据
const dataSource = ref([])
const dataSourceArea = ref([])
const dataSourceArea2 = ref([])
function getTree() {
    getTreeList().then(res => {
        dataSource.value = res.data
    })
}
getTree()

function treeChange(id) {
    queryParams.value.workArea = ''
    dataSourceArea.value = []
    if(id == null || id == undefined || id == ''){
        return
    }
    getDeptBy({
        parentId: id
    }).then(res => {
        dataSourceArea.value = res.data
    })
}
function treeChange2(id) {
    form.value.workArea = ''
    dataSourceArea2.value = []
    getDeptBy({
        parentId: id
    }).then(res => {
        dataSourceArea2.value = res.data
    })
}


/** 查询公告列表 */
function getList() {
    loading.value = true;
    listUpperLocation(queryParams.value).then(response => {
        list.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 查看按钮操作 */
function handleView(row) {
    reset();
    const id = row.id;
    getUpperLocation(id).then(response => {
        form.value = response.data;
        mapNum.value = form.value.paths.length;
        lineData.value = form.value.paths;
        tempForm.value = form.value;
        console.log(" response.data", response.data);
        viewFlag.value = true
        open.value = true;
        title.value = "应急路径";

        // console.log("dataArr",dataArr.value)
    });
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    viewFlag.value = false
    title.value = "应急路径/上道地点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const id = row.id;
    if(row.section){
        treeChange2(row.section)
    }
    getUpperLocation(id).then(response => {
        form.value = response.data;
        viewFlag.value = false
        open.value = true;
        title.value = "应急路径";
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id;
    proxy.$modal.confirm('确定要删除当前上道地点？').then(function () {
        return delUpperLocation(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        workAreaName: undefined,
        upperLocaltion: undefined,
        lineType: undefined,
        kilometer: undefined,
        remark: undefined,
    };
    proxy.resetForm("configRef");
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["configRef"].validate(valid => {
        if (valid) {
            form.value.submited = 1;
            if (form.value.id != undefined) {
                updateUpperLocation(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addUpperLocation(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}
function delMapPath(item) {
    tempForm.value = form.value
    form.value.paths = form.value.paths.filter(
        i =>
            i.id !== item.id
    )
}

function addMapPath(){

}
getList();
</script>

<style lang="scss" scoped>
.layout-padding {
    padding: 10px;
}

#lineMap {
    width: 100%;
    height: 100%;
}

.lmap-image {
    width: 32px;
    height: 32px;
}

.lmap-span {
    display: inline-block;
    margin-left: 5px;
    padding: 5px;
    font-weight: bold;
    /* 	background: #66cdb1; */
    line-height: 20px;
    font-size: 14px;
    color: #fff;
    white-space: nowrap;
}

.lmap-text {
    display: inline-block;
    margin-left: 5px;
    padding: 5px;
    font-weight: bold;
    /* 	background: #66cdb1; */
    line-height: 20px;
    font-size: 16px;
    color: #fff;
    width: 500px;
    white-space: nowrap;
    position: absolute;
    text-align: center;
    top: 25px;
    left: -250px;
}

.mapBox {
    width: 740px;
    height: 230px;
    margin-bottom: 50px;
}

.mapDel {
    float: right;
}
</style>
